package Tydzien9;

import java.awt.*;
import java.awt.event.*; 
import java.applet.*;

public class MyAppletZdarzenia2 extends Applet implements ItemListener {  

	private static final long serialVersionUID = -7918403615234004471L;
	
	SimpleRepaintHandler repaintHandler = new SimpleRepaintHandler();
	
	CheckboxGroup checkboxGroup = new CheckboxGroup();
	Checkbox[] checkboxes = new Checkbox[4];
	Checkbox italicCheckbox = new Checkbox("Italic", false);
	Checkbox boldCheckbox = new Checkbox("Bold", false);
	Choice fontNames = new Choice();
	Choice fontColors = new Choice();

	int[] fontSizes = {18, 24, 32, 36};
	int fontSizeIndex = 0;
	Color fontColor = Color.blue;
	
	public void paint (Graphics g) {
		// Ustawia kroj i parametry czcionki w applecie 
		int fontFlags = (italicCheckbox.getState() ? Font.ITALIC : 0) + 
						(boldCheckbox.getState() ? Font.BOLD : 0);
		Font appFont = new Font(fontNames.getSelectedItem(), fontFlags, fontSizes[fontSizeIndex]); 
		g.setColor(fontColor); 
		g.setFont(appFont); 
		g.drawString("TEKST probny JAVA", 20, 100); 
	}
	
	public void init(){	
		
		resize(800, 150);
		
    	for(int i = 0; i < 4; ++i)
    	{
    		checkboxes[i] = new Checkbox(Integer.toString(fontSizes[i]) + "pt", false, checkboxGroup);
    		add(checkboxes[i]);
            checkboxes[i].addItemListener(this);
    	}
    	checkboxes[0].setState(true);
    	
    	add(italicCheckbox);
    	italicCheckbox.addItemListener(repaintHandler);
    	add(boldCheckbox);
    	boldCheckbox.addItemListener(repaintHandler);
    	
    	add(fontNames);
    	fontNames.add("Times New Roman");
    	fontNames.add("Arial");
    	fontNames.add("Courier New");
    	fontNames.add("Tahoma");
    	fontNames.add("Verdana");
    	fontNames.add("Lucida");
    	fontNames.add("Comic");
    	fontNames.addItemListener(repaintHandler);

    	add(fontColors);
    	fontColors.add("Blue");
    	fontColors.add("Red");
    	fontColors.add("Black");
    	fontColors.add("Cyan");
    	fontColors.add("Green");
    	fontColors.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
	    		switch(fontColors.getSelectedIndex())
	    		{
	    			case 0: fontColor = Color.blue; break;
	    			case 1: fontColor = Color.red; break;
	    			case 2: fontColor = Color.black; break;
	    			case 3: fontColor = Color.cyan; break;
	    			case 4: fontColor = Color.green; break;
	    			default: fontColor = Color.blue; break;
	    		}
	    		repaint();
			}
    	});
	}


	@Override
	public void itemStateChanged(ItemEvent e) {
		
		for(int i = 0; i < 4; ++i)
    	{
    		if(e.getItemSelectable() == checkboxes[i])
        	{
    			fontSizeIndex = i;
    			break;
        	}
    	}
		
		repaint();
	}
	
}		// koniec klasy Appletu
